package com.lc.recovery.ui.activity;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;

public class RechargeActivity extends AppActivity {


    @BindView(R.id.title_bar_top)
    TitleBar titleBarTop;
    @BindView(R.id.webview)
    WebView webview;

    private String title;
    private String url;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        Bundle bundle = getIntent().getExtras();


        url = bundle.getString("Url");
        title = bundle.getString("title");
        Log.e("TAG", "onCreate: "+title );
         titleBarTop.setTitle(title);
        titleBarTop.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
        loadurl();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recharge;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }


    private void loadurl() {


//        webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
//        webview.getSettings().setJavaScriptEnabled(true);

        webview.loadUrl(url);
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptEnabled(true);
        //WebView加载页面优先使用缓存加载
//        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        //设置支持缩放
//        webSettings.setBuiltInZoomControls(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.supportMultipleWindows();
        webSettings.setAllowContentAccess(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSavePassword(true);
        webSettings.setSaveFormData(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLoadsImagesAutomatically(true);

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候是控制网页在WebView中去打开，如果为false调用系统浏览器或第三方浏览器打开
                view.loadUrl(url);
                return true;
            }
            //WebViewClient帮助WebView去处理一些页面控制和请求通知
        });
        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onCloseWindow(WebView window) {
                super.onCloseWindow(window);

                finish();


            }

            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {//5.0+

                Log.e("llq", "onShowFileChooser");
//              photoDialog.show();


//
                return true;
            }

            //openFileChooser 方法是隐藏方法
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {// android 系统版本>4.1.1
                Log.e("llq", "onShowFileChooser");


            }


        });


        webview.setWebViewClient(new WebViewClient() {


            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);


            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);
                Log.d("Jun", url);

//                if (Build.VERSION.SDK_INT<26) {
//                    view.loadUrl(url);
//WebView.HitTestResult hit=view.getHitTestResult();
//
//                Log.e(TAG, "shouldOverrideUrlLoading: "+url+"-----"+hit );
//if (hit!=null){
//    flag=false;
//    return false;
//}else {
//    flag=true;
//}


                view.loadUrl(url);

//                if (title.equals("充值")) {
//                    new Handler().postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            webview.goBack();
//                        }
//                    }, 4000);
//                }
                return true;
            }
//                return true;
//            }

        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//
        if (keyCode == 4 && webview.canGoBack()) {
            if (webview.canGoBack()) {
                webview.goBack();
                return true;
            } else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {

    }


    final class JSAndroidPay {


        public JSAndroidPay() {

        }


    }


}















